local math_modf = math.modf 
local mt = ac.unit.__index 
local hex = (require 'util.color').hex

-- 特殊字符 字体已经被修改为 特殊字符
-- local str = [[
--     |cffff4848①暴击+100|r 
--     |cfff054f5②移速+100|r 
--     |cff44ff44③生命+100|r 
--     |cffffa244④射程+100|r 
--     |cfffb6e6e⑤吸血+100|r 
--     |cff9151ff⑥法强+100|r
--     |cff2aadf9⑦魔法上限+100|r
--     |cffce1818⑧攻击力+100|r 
--     |cff44ff44⑨生命恢复+100|r 
--     |cff2aadf9⑩魔法恢复+100|r 
--     |cfffefa87⑴攻击速度+100|r 
--     |cfff92a5e⑵冷却缩减+100|r 
--     |cffe2d4b6⑶护甲+100|r 
--     |cffea8481⑷护甲穿透+100|r 
--     |cff6ca6c8⑸魔抗+100|r 
--     |cff936edc⑹法术穿透+100|r 
--     |cffffcc00⑺金币+100|r
--     |cff48ff4b⑻木材+100|r
-- ]]

function mt:jump_text(text, red, green, blue, alpha)
    return ac.texttag {
        target = self,
        text = text,
        color = hex(red or 221, green or 218, blue or 190, 255),
        alpha = alpha or 255,
        show_flag = self:get_owner():is_self(),
        fadepoint = 1,
        lifespan = 2,
        yvel = 45,
    }
end

function mt:jump_gold(value)
    return ac.texttag {
        target = self,
        font_size = 24,
        text = value >= 0 and ('+%d'):format(value) or ('-%d'):format(math.abs(value)),
        xvel = 0,
        yvel = 30,
        color = 0xffffff00,
        show_flag = self:get_owner():is_self(),
        fadepoint = 1,
        lifespan = 2,
    }
end

function mt:jump_wood(value)
    return ac.texttag {
        target = self,
        font_size = 24,
        text = value >= 0 and ('+%d'):format(value) or ('-%d'):format(math.abs(value)),
        xvel = 0,
        yvel = 30,
        color = 0xff0088ff,
        show_flag = self:get_owner():is_self(),
        fadepoint = 1,
        lifespan = 2,
    }
end

function mt:jump_exploit(value)
    return ac.texttag {
        target = self,
        font_size = 24,
        text = value >= 0 and ('+%d'):format(value) or ('-%d'):format(math.abs(value)),
        xvel = 0,
        yvel = 30,
        color = 0xff6fff6f,
        show_flag = self:get_owner():is_self(),
        fadepoint = 1,
        lifespan = 2,
    }
end

function mt:jump_gold_wood(gold, wood)
    return ac.texttag {
        target = self,
        font_size = 24,
        text = ('|cffffff00+%d|r |cff0088ff+%d|r'):format(gold, wood),
        xvel = 0,
        yvel = 30,
        show_flag = self:get_owner():is_self(),
        fadepoint = 1,
        lifespan = 2,
    }
end

function mt:jump_damage(text)
    return ac.texttag {
        target = self,
        text = text,
        show_flag = self:is_visible(ac.player.self),
        color = 0xffff1616,
        fadepoint = 0.25,
        lifespan = 1,
        yvel = 60,
    }
end

function mt:jump_missing()
    return ac.texttag {
        target = self,
        text = 'miss',
        show_flag = self:is_visible(ac.player.self),
        color = 0xffff1616,
        fadepoint = 0.25,
        lifespan = 1,
        yvel = 75,
    }
end

function mt:jump_heal(text)
    return ac.texttag {
        target = self:get_point(),
        text = text,
        show_flag = self:is_visible(ac.player.self),
        color = 0xff6bff6b,
        fadepoint = 0.25,
        lifespan = 1,
        xvel = math.random(-45, 45),
        yvel = math.random(-45, 45),
    }
end

function mt:jump_message(text)
    return ac.texttag {
        target = self,
        text = text,
        color = 0xffffcc00,
        alpha = 255,
        show_flag = self:is_visible(ac.player.self),
        fadepoint = 0.25,
        lifespan = 1,
        yvel = 25,
    }
end

function ac.point.__index:jump_message(text)
    return ac.texttag {
        target = self,
        text = text,
        color = 0xffffcc00,
        alpha = 255,
        height = 32,
        show_flag = self:is_visible(ac.player.self),
        fadepoint = 0.25,
        lifespan = 1,
        yvel = 25,
    }
end

function mt:jump_crit_damage(text, color)
    local scale = 0.45
    local size1 = 18.0
    local size2 = 32.0
    local size3 = 20.0
    local angle = 180 * math.random(1,2)
    local start = self:get_point()
    local final = start - {angle, math.random(32, 64)}
    local bezp1 = start:lerp(final, scale)
    local height = self:get_height() + ac.message.unit_overhead(self.handle)
    bezp1.z = math.random(32, 64)
    ac.texttag {
        text = ('%s'):format(text),
        target = start,
        height = height,
        font_size = size1,
        show_flag = self:is_visible(ac.player.self),
        color = color,
        fadepoint = 0.5,
        lifespan = 1,
        delay = 0,
        on_update = function(tag, elapse)
            tag.delay = tag.delay + elapse
            if tag.delay <= 1 then 
                local p1 = start:lerp(bezp1, tag.delay)
                local p2 = bezp1:lerp(final, tag.delay)
                local p3 = p1:lerp(p2, tag.delay)
                local s1 = math.lerp(size1,  size2, tag.delay)
                local s2 = math.lerp(size2,  size3, tag.delay)
                local s3 = math.lerp(s1,  s2, tag.delay)
                tag:set_font_size( s3 )
                tag:set_tagpos( p3, height + p3.z )
            else
                tag:remove()
            end 
        end ,
    }
end

ac.game:on '单位-受到伤害结束' (function(_, unit, source, damage)
    if damage:is_crit() and source:is_hero() then 
        local value = math_modf(damage:get_current_damage())
        if value > 0 then 
            if damage:is_common_attack() then 
                unit:jump_crit_damage(("① %d"):format(value), 0xffff1616)
            elseif damage:is_type("法术") then 
                unit:jump_crit_damage(("① %d"):format(value), 0xff00bbff)
            end 
        end 
    end 
end)

ac.game:on '单位-受到治疗' (function(_, unit, source, heal)
    if not heal.item then return end
    local uclock = unit:get_data("heal-texttag-cool") or 0 
    local nclock = ac.clock() / 1000
    if (nclock - uclock) > 0.7 then 
        unit:set_data("heal-texttag-cool", nclock)
        local value = math_modf(heal.heal)
        if value >= 1 then 
            unit:jump_heal(("+%d"):format(value))
        end 
    end 
end)

ac.game:on '单位-闪避攻击成功' (function(_, unit, source, damage)
    unit:jump_missing()
end)

ac.game:on '单位-免疫控制效果' (function(_, unit, source, damage)
    ac.texttag {
        target = unit,
        text = "控制免疫",
        color = 0xff00eeff,
        alpha = 255,
        show_flag = unit:is_visible(ac.player.self),
        fadepoint = 0.25,
        lifespan = 1,
        yvel = 25,
    }
end)

function ac.unit.__index:add_gold(gold)
    local owner = self:get_owner()
    local point = self:get_point()
    self:jump_gold(gold)
    owner:add('金币', gold)
    point:play_sound('金币')
    point:add_effect('UI\\Feedback\\GoldCredit\\GoldCredit.mdl')
        :set_facing(270)
        :set_height(point:get_height())
        :remove()
end 

function ac.unit.__index:add_lumber(lumber)
    local owner = self:get_owner()
    local point = self:get_point()
    self:jump_wood(lumber)
    owner:add('晶石', lumber)
    point:play_sound('金币')
    point:add_effect('crystalcredit.mdl')
        :set_facing(270)
        :set_height(point:get_height())
        :remove()
end 
